import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'pages/Tabs.dart';
import 'utils/HexColor.dart';

void main() {
  runApp(MaterialApp(
    title: "login",
    //去掉右上角的debug标识
    debugShowCheckedModeBanner: false,
    home: Scaffold(
      //隐藏感叹提醒 测试版 true
      resizeToAvoidBottomInset: false,
      body: ContainerLayout(),
    ),
  ));
}

class ContainerLayout extends StatelessWidget {
  final usernameTextEditController = TextEditingController();
  final passwordTextEditController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      decoration: const BoxDecoration(
          image: DecorationImage(
              image: AssetImage("assets/images/login_bg.png"),
              fit: BoxFit.fill)),
      child: Padding(
        padding: const EdgeInsets.fromLTRB(0, 94, 0, 0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset("assets/images/logo.png", width: 56, height: 56),
            const Padding(
              padding: EdgeInsets.fromLTRB(0, 12, 0, 0),
              child: Text(
                "欢迎登录河南省总工会",
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(22, 76, 22, 0),
              child: Container(
                decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(16),
                  ),
                  boxShadow: [
                    BoxShadow(
                        color: Colors.black12, spreadRadius: 6, blurRadius: 10),
                  ],
                ),
                child: Padding(
                  padding: const EdgeInsets.fromLTRB(18, 0, 18, 0),
                  child: TextField(
                    controller: usernameTextEditController,
                    style: const TextStyle(fontSize: 16),
                    decoration: InputDecoration(
                        hintText: "请输入账号",
                        hintStyle: TextStyle(color: HexColor("#C4C9CF")),
                        icon: Image.asset(
                          "assets/images/account.png",
                          height: 18,
                          width: 18,
                          color: HexColor("#C4C9CF"),
                        ),
                        border: InputBorder.none),
                  ),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(22, 30, 22, 0),
              child: Container(
                decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(16),
                  ),
                  boxShadow: [
                    BoxShadow(
                        color: Colors.black12, spreadRadius: 6, blurRadius: 10),
                  ],
                ),
                child: Padding(
                  padding: const EdgeInsets.fromLTRB(18, 0, 18, 0),
                  child: showOrHideTextField(passwordTextEditController),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(22, 12, 22, 0),
              child: Container(
                width: MediaQuery.of(context).size.width,
                child: Text(
                  "忘记密码？",
                  style: TextStyle(fontSize: 14, color: HexColor("#8A939E")),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(22, 40, 22, 0),
              child: GestureDetector(
                onTap: () {
                  if (usernameTextEditController.value.text.isNotEmpty) {
                    if (passwordTextEditController.value.text.isNotEmpty) {
                      Navigator.push(context,
                          MaterialPageRoute(builder: (context) {
                        return Tabs();
                      }));
                    } else {
                      Fluttertoast.showToast(
                          msg: "请输入密码",
                          toastLength: Toast.LENGTH_SHORT,
                          gravity: ToastGravity.BOTTOM,
                          backgroundColor: Colors.black,
                          textColor: Colors.white,
                          fontSize: 16.0,
                          webShowClose: true,
                          webBgColor: "#000000",
                          webPosition: "center");
                    }
                  } else {
                    Fluttertoast.showToast(
                        msg: "请输入账号",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        backgroundColor: Colors.black,
                        textColor: Colors.white,
                        fontSize: 16.0,
                        webShowClose: true,
                        webBgColor: "#000000",
                        webPosition: "center");
                  }
                },
                child: Container(
                  decoration: BoxDecoration(
                    color: HexColor("#EF3A43"),
                    borderRadius: const BorderRadius.all(
                      Radius.circular(16),
                    ),
                  ),
                  height: 54,
                  width: MediaQuery.of(context).size.width,
                  child: const Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        "登录",
                        style: TextStyle(fontSize: 16, color: Colors.white),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class showOrHideTextField extends StatefulWidget {
  TextEditingController passwordTextEditController;

  showOrHideTextField(this.passwordTextEditController);

  @override
  State<StatefulWidget> createState() {
    return showOrHideTextFieldStates(passwordTextEditController);
  }
}

class showOrHideTextFieldStates extends State<showOrHideTextField> {
  var flag = false;
  TextEditingController passwordTextEditController;

  showOrHideTextFieldStates(this.passwordTextEditController);

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Expanded(
          child: TextField(
            controller: passwordTextEditController,
            style: const TextStyle(fontSize: 16),
            obscureText: !flag,
            decoration: InputDecoration(
                hintText: "请输入密码",
                hintStyle: TextStyle(color: HexColor("#C4C9CF")),
                icon: Image.asset(
                  "assets/images/password.png",
                  height: 18,
                  width: 18,
                  color: HexColor("#C4C9CF"),
                ),
                border: InputBorder.none),
          ),
        ),
        GestureDetector(
          child: Image.asset(
            !flag
                ? "assets/images/hide_password.png"
                : "assets/images/show_password.png",
            height: 18,
            width: 18,
            color: HexColor("#8A939E"),
          ),
          onTap: () {
            setState(() {
              flag = !flag;
            });
          },
        )
      ],
    );
  }
}

class MyHomeBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width,
          child: Image.asset("images/login_bg.png", fit: BoxFit.fill),
        ),
        Padding(
          padding: const EdgeInsets.fromLTRB(0, 94, 0, 0),
          child: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Image.asset("images/logo.png", width: 56, height: 56),
                Padding(
                    padding: const EdgeInsets.fromLTRB(0, 12, 0, 0),
                    child: Text("欢迎登录河南省总工会",
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.w800))),
              ],
            ),
          ),
        )
      ],
    );
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Image.asset("images/login_bg.png", fit: BoxFit.fill),
    );
  }
}

/*class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        //去掉右上角的debug标识
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          appBar: AppBar(
            title: Text("TEST"),
          ),
          body: HomeListContent(),
        ));
  }
}

class HomeListContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.all(8.0),
        child: ListView(
          children: [
            ItemWidget("Apple1", "Macbook Product1",
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F180528b1-7f55-4667-aa52-638b4c85abfa%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1693634658&t=06d00eb963b85bb15ad5496cd30ba25e"),
            ItemWidget("Apple2", "Macbook Product2",
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F180528b1-7f55-4667-aa52-638b4c85abfa%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1693634658&t=06d00eb963b85bb15ad5496cd30ba25e"),
            ItemWidget("Apple3", "Macbook Product3",
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F180528b1-7f55-4667-aa52-638b4c85abfa%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1693634658&t=06d00eb963b85bb15ad5496cd30ba25e")
          ],
        ));
  }
}

class ItemWidget extends StatelessWidget {
  final String title, Content, Url;

  ItemWidget(this.title, this.Content, this.Url);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(20),
      decoration: BoxDecoration(
        border: Border.all()
      ),
      child: Column(
        children: <Widget>[
          Text(title, style: TextStyle(fontSize: 24)),
          Text(Content, style: TextStyle(fontSize: 18)),
          SizedBox(height: 10),
          Image.network(Url)
        ],
      ),
    ) ;
  }
}*/

/*class HomeContent extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyChildStates("标题","内容","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F180528b1-7f55-4667-aa52-638b4c85abfa%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1693634658&t=06d00eb963b85bb15ad5496cd30ba25e");
  }
}

class MyChildStates extends State<HomeContent> {
  var flag = true;
  var i = 0;
  final String title, content, Url;

  MyChildStates(this.title, this.content, this.Url);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Checkbox(
              value: flag,
              onChanged: (value) {
                this.setState(() {
                  flag = value!;
                  i++;
                });
              }),
          Text("同意该不平等协议", style: TextStyle(fontSize: 20)),
          Text(title+i.toString(),style: TextStyle(fontSize: 24)),
          Text(content+i.toString(),style: TextStyle(fontSize: 18)),
          Image.network(Url)
        ],
      ),
    );
  }
}*/
